// FrogBaseDialog.h : Declaration of the CFrogBaseDialog

#pragma once

#include "resource.h"       // main symbols
#include <atlhost.h>
#include <list>
#include "FrogMessageBox.h"
#include "DialogMessageHook.h"
#include "../FilerFrogUtils/FilerFrogUtils.h"

using namespace std;

// CFrogBaseDialog
template <class T>
class CFrogBaseDialog : 
	public CDialogImpl<T>	
{
public:

	bool m_IsModal;

	CFrogBaseDialog(HICON hIcon):
	  m_hIcon(NULL),
		  m_hDisplayFont(NULL),
		  m_IsModal(false)
	  {
		  SetIcon(hIcon);

		  m_hDisplayFont = CreateFont(-12, 0, 0, 0, FW_BOLD, FALSE, FALSE, FALSE, OEM_CHARSET,
			  OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
			  DEFAULT_PITCH | FF_DONTCARE, L"Tahoma");
	  }

	  void SetIcon(HICON hIcon)
	  {
		  if (m_hIcon)
		  {
			  DestroyIcon(m_hIcon);
			  m_hIcon = NULL;
		  }	

		  if (hIcon)
		  {
			  m_hIcon = DuplicateIcon(NULL, hIcon);
		  }
	  }

	  virtual ~CFrogBaseDialog()
	  {
		  if (m_hDisplayFont)
		  {
			  DeleteObject(m_hDisplayFont);
			  m_hDisplayFont = NULL;
		  }

		  if (m_hIcon)
		  {
			  DestroyIcon(m_hIcon);
			  m_hIcon = NULL;
		  }	
	  }

	  INT_PTR DoModal(HWND hWndParent = ::GetActiveWindow(), LPARAM dwInitParam = NULL)
	  {
		  m_IsModal = true;
		  return CDialogImpl::DoModal(hWndParent, dwInitParam);
	  }

	  virtual void OnFinalMessage(HWND /*hWnd*/)
	  {
		  if (!m_IsModal)
		  {
			  delete this;
		  }		  
	  }

	  //enum { IDD = 0 };

	  BEGIN_MSG_MAP(T)
		  MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
		  MESSAGE_HANDLER(WM_CTLCOLORSTATIC, OnCtlColorStatic)
		  COMMAND_HANDLER(IDOK, BN_CLICKED, OnClickedOK)
		  COMMAND_HANDLER(IDCANCEL, BN_CLICKED, OnClickedCancel)
	  END_MSG_MAP()

	  // Handler prototypes:
	  //  LRESULT MessageHandler(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	  //  LRESULT CommandHandler(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
	  //  LRESULT NotifyHandler(int idCtrl, LPNMHDR pnmh, BOOL& bHandled);

	  LRESULT OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	  {		
		  bHandled = TRUE;
		  InitWhiteBar();

		  if (m_hIcon != NULL)
		  {
			  SendMessage(WM_SETICON, ICON_BIG, (LPARAM)m_hIcon);
			  SendMessage(WM_SETICON, ICON_SMALL, (LPARAM)m_hIcon);
		  }

		  if (!m_IsModal)
		  {
			  CDialogMessageHook::InstallHook(m_hWnd);
		  }		

		  return bHandled;  // Let the system set the focus
	  }

	  void InitWhiteBar()
	  {	
		  RECT windowRect;
		  GetWindowRect(&windowRect);

		  int w = windowRect.right - windowRect.left + 10;
		  int h = 60;
		  int x = -5;
		  int y = -5;

		  HWND hwndWhiteRect = GetDlgItem(IDC_STATIC_WHITE_RECT);
		  ::SetWindowPos(hwndWhiteRect, HWND_BOTTOM, x, y, w, h, SWP_NOZORDER);

		  HWND hwndTitle = GetDlgItem(IDC_STATIC_TITLE);
		  SendMessage(hwndTitle, WM_SETFONT, (WPARAM)m_hDisplayFont, TRUE);
	  }

	  LRESULT OnCtlColorStatic(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	  {
		  bHandled = true;

		  if (::GetDlgCtrlID((HWND)lParam) == IDC_STATIC_TITLE)
		  {				
			  SetBkMode((HDC)wParam, TRANSPARENT);				
			  return (BOOL)GetStockObject(NULL_BRUSH);
		  }
		  else
		  {
			  return FALSE;
		  }
	  }	

	  LRESULT OnClickedOK(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
	  {
		  EndDialog(wID);

		  return 0;
	  }

	  LRESULT OnClickedCancel(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
	  {
		  EndDialog(wID);

		  return 0;
	  }

	  BOOL EndDialog(int nRetCode)
	  {	
		  if (m_IsModal)
		  {
			  CDialogImpl::EndDialog(nRetCode);
		  }
		  else
		  {
			  CDialogMessageHook::UninstallHook(m_hWnd);
			  DestroyWindow();
		  }

		  return 0;
	  }

private:
	HICON m_hIcon;
	HFONT m_hDisplayFont;
};
